const fs = require('fs')
const path = require('path')
const BASE_GRAPHQL_DIR = path.join(__dirname, '../graphql')

const initGraphql = (path = BASE_GRAPHQL_DIR) => {
    // 保存所有Graphql Schema
    let schemas = []
    const files = fs.readdirSync(path, { encoding: 'utf-8' })
    if(files.length === 0) {
        throw new Error('请添加Graphql')
    }
    while (files.length > 0) {
        const file = files.shift()
        const schema = fs.readFileSync(`${BASE_GRAPHQL_DIR}/${file}`, { encoding: 'utf-8' })
        schemas.push(schema)
    }
    return schemas.join('')
}

module.exports = { initGraphql }